package configx

import (
	"github.com/spf13/viper"
)

type Config struct {
	App    AppConfig    `mapstructure:"app"`
	Grpc   GrpcConfig   `mapstructure:"grpc"`
	Mysql  MysqlConfig  `mapstructure:"mysql"`
	Minio  MinioConfig  `mapstructure:"minio"`
	Http   HttpConfig   `mapstructure:"http"`
	Email  EmailConfig  `mapstructure:"email"`
	Consul ConsulConfig `mapstructure:"consul"`
	Jwt    JwtConfig    `mapstructure:"jwt"`
	Sentry SentryConfig `mapstructure:"sentry"`
}

type AppConfig struct {
	Name  string `mapstructure:"name"`
	Debug bool   `mapstructure:"debug"`
}

type GrpcConfig struct {
	Address  string `mapstructure:"address"`
	Port     int    `mapstructure:"port"`
	CertFile string `mapstructure:"cert_file"`
	KeyFile  string `mapstructure:"key_file"`
}

type MysqlConfig struct {
	Dns string `mapstructure:"dns"`
}

type MinioConfig struct {
	Endpoint        string `mapstructure:"endpoint"`
	AccessKeyID     string `mapstructure:"access_key_id"`
	SecretAccessKey string `mapstructure:"secret_access_key"`
	UseSSL          bool   `mapstructure:"use_ssl"`
	BucketName      string `mapstructure:"bucket_name"`
	Location        string `mapstructure:"location"`
	UseProxy        bool   `mapstructure:"use_proxy"`
	ProxyUrl        string `mapstructure:"proxy_url"`
}

type HttpConfig struct {
	Address string `mapstructure:"address"`
	Port    int32  `mapstructure:"port"`
}

type EmailConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
}

type ConsulConfig struct {
	Address string `mapstructure:"address"`
	Port    int    `mapstructure:"port"`
}

type JwtConfig struct {
	Secret       string `mapstructure:"secret"`
	ExpiredHours int64  `mapstructure:"expired_hours"`
}

type SentryConfig struct {
	Dns              string  `mapstructure:"dns"`
	EnableTracing    bool    `mapstructure:"enable_tracing"`
	TracesSampleRate float64 `mapstructure:"traces_sample_rate"`
}

var C Config

// Init 初始化配置
func Init(path string) error {
	viper.SetConfigFile(path)
	if err := viper.ReadInConfig(); err != nil {
		return err
	}
	if err := viper.Unmarshal(&C); err != nil {
		return err
	}
	return nil
}
